package com.taptech.doufu.weex.statusbar;

import android.app.Activity;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.taobao.weex.utils.WXResourceUtils;
import com.taptech.doufu.ui.activity.weex.TFWXActivity;
import com.taptech.doufu.weex.TFStatusBarUtil;
import com.taptech.doufu.app.WeMediaApplication;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 适配小米
 */

public class StatusBarCompatFlavorRom {

    interface ILightStatusBar {
        void setLightStatusBar(Activity activity,Window window,boolean isSetAgain, boolean lightStatusBar);
    }

    static final ILightStatusBar IMPL;

    static {
        if (MIUILightStatusBarImpl.isMe()) {
            IMPL = new MIUILightStatusBarImpl();
        } else if (MeizuLightStatusBarImpl.isMe()) {
            IMPL = new MeizuLightStatusBarImpl();
        } else {
            IMPL = new ILightStatusBar() {
                @Override
                public void setLightStatusBar(Activity activity,Window window,boolean isSetAgain, boolean lightStatusBar) {
                    if (activity != null && activity instanceof TFWXActivity) {
                        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LAYOUT_STABLE|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                        if (isSetAgain) {
                            if (WeMediaApplication.getInstance().isDayNightMode_Night){
                                StatusBarCompat.setStatusBarColor(activity, WXResourceUtils.getColor("#212222"), false);
                            }else {
                                StatusBarCompat.setStatusBarColor(activity, WXResourceUtils.getColor("#ffffff"), true);
                            }
                        }else {
                            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                            TFStatusBarUtil.transparencyBar(activity);
                        }
                    }
                }
            };
        }
    }

    public  static void setLightStatusBar(Window window, boolean lightStatusBar) {
        IMPL.setLightStatusBar(null,window,true, lightStatusBar);
    }

    public  static void setLightStatusBar(Activity activity, boolean lightStatusBar) {
        IMPL.setLightStatusBar(activity,activity.getWindow(),true, lightStatusBar);
    }
    public  static void setLightStatusBar(Activity activity,boolean isSetAgain, boolean lightStatusBar) {
        IMPL.setLightStatusBar(activity,activity.getWindow(),isSetAgain, lightStatusBar);
    }

    static class MIUILightStatusBarImpl implements ILightStatusBar {
        static boolean isMe() {
            return "Xiaomi".equals(Build.MANUFACTURER);
        }

        public void setLightStatusBar(Activity activity,Window window,boolean isSetAgain, boolean lightStatusBar) {
            Class<? extends Window> clazz = window.getClass();
            try {
                Class<?> layoutParams;
                Field field;
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N  &&
                        activity != null && activity instanceof TFWXActivity){
                    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LAYOUT_STABLE|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                    if (isSetAgain) {
                        if (WeMediaApplication.getInstance().isDayNightMode_Night){
                            StatusBarCompat.setStatusBarColor(activity, WXResourceUtils.getColor("#212222"), false);
                        }else {
                            StatusBarCompat.setStatusBarColor(activity, WXResourceUtils.getColor("#ffffff"), true);
                        }
                    }else {
                        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                        TFStatusBarUtil.transparencyBar(activity);
                    }
                }else{
                    layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                    field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                    int darkModeFlag = field.getInt(layoutParams);
                    Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
                    extraFlagField.invoke(window, lightStatusBar ? darkModeFlag : 0, darkModeFlag);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    static class MeizuLightStatusBarImpl implements ILightStatusBar {
        static boolean isMe() {
            return Build.BRAND.contains("Meizu");
        }

        public void setLightStatusBar(Activity activity,Window window,boolean isSetAgain, boolean lightStatusBar) {
            WindowManager.LayoutParams params = window.getAttributes();
            try {
                Field darkFlag = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags");
                darkFlag.setAccessible(true);
                meizuFlags.setAccessible(true);
                int bit = darkFlag.getInt(null);
                int value = meizuFlags.getInt(params);
                if (lightStatusBar) {
                    value |= bit;
                } else {
                    value &= ~bit;
                }
                meizuFlags.setInt(params, value);
                window.setAttributes(params);
                darkFlag.setAccessible(false);
                meizuFlags.setAccessible(false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
